using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace Contact_Manager
{
    class Contact
    {
        private string line;
        public string Line
        {
            get { return line; }
            set { line = value; }
        }

        private string delimits;
        public string Delimiters
        {
            get { return delimits; }
            set { delimits = value; }
        }

        private ArrayList names;
        public ArrayList Names
        {
            get { return names; }
            set { names = value; }
        }

        private ArrayList emails;
        public ArrayList Emails
        {
            get { return emails; }
            set { emails = value; }
        }

        private ArrayList phones;
        public ArrayList Phones
        {
            get { return phones; }
            set { phones = value; }
        }

        private ArrayList descripts;
        public ArrayList Descripts
        {
            get { return descripts; }
            set { descripts = value; }
        }


        public void AddName(string s)
        {
            names.Add(s);
        }

        public void AddEmail(ArrayList a)
        {
            emails.AddRange(a.ToArray());
        }

        public void AddEmail(string s)
        {
            emails.Add(s);
        }

        public void AddPhone(ArrayList a)
        {
            phones.AddRange(a.ToArray());
        }
        
        public void AddPhone(string s)
        {
            phones.Add(s);
        }

        public void AddDescript(string s)
        {
            descripts.Add(s);
        }

        //private string EmailPattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
        //       @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" +
        //       @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
        private string EmailPattern = @"[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" +
               @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*(\.[a-z][a-z|0-9]*)+";
        private string PhonePattern = @"[-|0-9|\s]{6,}$";
        private string NamePattern = @"[\s|a-z]+";
        private string DescriptPattern = @"[0-9|a-z]+";


        public Contact(string str, string delimiters)
        {
            Line = str;
            Delimiters = delimiters;
            Names = new ArrayList();
            Emails = new ArrayList();
            Phones = new ArrayList();
            Descripts = new ArrayList();

            string[] s = line.Split(delimiters.ToCharArray());
            for (int i = 0; i < s.Length; i++)
                if (s[i].Trim() != "")
                {
                    string word = s[i].Trim();                    
                    ArrayList arr;

                    arr = EmailFormat(ref word);
                    if (arr.Count > 0) AddEmail(arr);

                    arr = PhoneFormat(ref word);
                    if (arr.Count > 0) AddPhone(arr);

                    if (names.Count == 0)
                    {
                        string name=NameFormat(ref word); 
                        if (name != "") AddName(name);
                    }

                    if (DescriptFormat(word)) AddDescript(word);
                }
        }

        private string NameFormat(ref string s)
        {            
            Match ma = Regex.Match(s, NamePattern, RegexOptions.IgnoreCase);
            if (ma.Success) { s = s.Replace(ma.Value, ""); return ma.Value; }
            else return "";
        }

        private ArrayList EmailFormat(ref string s)
        {
            ArrayList em = new ArrayList();
            string str = s;
            Match ma = Regex.Match(s, EmailPattern, RegexOptions.IgnoreCase);
            while (ma.Success) 
            { em.Add(ma.Value); str = str.Replace(ma.Value, "");  ma = ma.NextMatch(); }
            s = str;
            return em;
        }

        private ArrayList PhoneFormat(ref string s)
        {
            ArrayList ph = new ArrayList();
            string str = s;
            Match ma = Regex.Match(s, PhonePattern, RegexOptions.IgnoreCase);
            while (ma.Success)
            { ph.Add(ma.Value); str = str.Replace(ma.Value, ""); ma = ma.NextMatch(); }
            s = str;
            return ph;
        }

        private bool DescriptFormat(string s)
        {
            if (Regex.IsMatch(s, DescriptPattern, RegexOptions.IgnoreCase)) return true;
            else return false;
        }

        public string ToString(string delimiter)
        {
            string s="";
            for (int i = 0; i < names.Count; i++)
                s += (string)names[i] + delimiter;
            for (int i = 0; i < emails.Count; i++)
                s += (string)emails[i] + delimiter;
            for (int i = 0; i < phones.Count; i++)
                s += (string)phones[i] + delimiter;
            for (int i = 0; i < descripts.Count; i++)
                s += (string)descripts[i] + delimiter;
            return s;
        }

        public string getName()
        {
            if (names.Count > 0) return (string)names[0];
            else return "";
        }

        public string getNames(string delimiter)
        {
            string s = "";
            for (int i = 0; i < Names.Count; i++)
                s += (string)Names[i] + delimiter;
            s = s.TrimEnd(delimiter.ToCharArray());
            return s;
        }
        
        public string getEmail()
        {
            if (emails.Count > 0) return (string)emails[0];
            else return "";
        }

        public string getEmails(string delimiter)
        {
            string s = "";
            for (int i = 0; i < emails.Count; i++)
                s += (string)emails[i] + delimiter;
            s = s.TrimEnd(delimiter.ToCharArray());
            return s;
        }

        public string getPhone()
        {
            if (phones.Count > 0) return (string)phones[0];
            else return "";
        }

        public string getPhones(string delimiter)
        {
            string s = "";
            for (int i = 0; i < phones.Count; i++)
                s += (string)phones[i] + delimiter;
            s = s.TrimEnd(delimiter.ToCharArray());
            return s;
        }

        public string getDescript()
        {
            if (descripts.Count > 0) return (string)descripts[0];
            else return "";
        }

        public string getDescripts(string delimiter)
        {
            string s = "";
            for (int i = 0; i < descripts.Count; i++)
                s += (string)descripts[i] + delimiter; 
            s =     s.TrimEnd(delimiter.ToCharArray());
            return s;
        }
    }
}
